<?php

namespace app\home\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class IndexModel extends Model
{
	/**
	*	首页轮播图
	*/
	public function getFlash()
	{
		$where = [];
		$where['status'] = 2;
		$result = Db::table('sz_flash')->where($where)->select();
		$data = [];
		foreach($result as $key=>$val){
			$data[$key]['id']   = $val['id'];
			$data[$key]['gid']  = $val['gid'];
			$data[$key]['pic']  = getPreviewUrl($val['pic']);
			$data[$key]['status']  = $val['status'];
			$data[$key]['addtime'] = $val['addtime'];
		}

		return $data;
	}

	/**
	*	首页团购商品
	*/
	public function getGroupGoods()
	{
		$where = [];
		$where['g_status'] = 2;
		$where['g_type'] = 1;
		$result = Db::table('sz_goods')->where($where)->select();

		$data = [];
		foreach($result as $key=>$val){
			$list[$key] = json_decode($val['g_pic'],true);
			foreach($list as $k1=>$v1){
				$res[$key]['photos'] = $v1['photos'];
				foreach($res[$key]['photos'] as $k2=>$v2){
					$arr['photos'][$k2]['url'] = getPreviewUrl($v2['url']);
					$data[$key]['g_pic'] = json_encode($arr);
				}
			}
			$data[$key]['id'] = $val['id'];
			$data[$key]['g_type'] = $val['g_type'];
			$data[$key]['g_name'] = $val['g_name'];
			$data[$key]['g_cprice'] = $val['g_cprice'];
			$data[$key]['g_oprice'] = $val['g_oprice'];
			$data[$key]['g_description'] = $val['g_description'];
			$data[$key]['g_content'] = $val['g_content'];
			$data[$key]['g_show'] = $val['g_show'];
			$data[$key]['g_tnumber'] = $val['g_tnumber'];
			$data[$key]['g_unit'] = $val['g_unit'];
			$data[$key]['g_number'] = $val['g_number'];
			$data[$key]['starttime'] = $val['starttime'];
			$data[$key]['endtime'] = $val['endtime'];
			$data[$key]['lefttime'] = $val['endtime'] - strtotime(date('Y-m-d H:i:s',time()));
			$data[$key]['addtime'] = $val['addtime'];
		}

		return $data;
	}

	/**
	*	首页广告
	*/
	public function getAdverts()
	{
		$where = [];
		$where['status'] = 2;
		$result = Db::table('sz_advertising')->where($where)->limit(4)->select();
		$data = [];
		foreach($result as $key=>$val){
			$data[$key]['id']   = $val['id'];
			$data[$key]['gid']  = $val['gid'];
			$data[$key]['pic']  = getPreviewUrl($val['pic']);
			$data[$key]['status']  = $val['status'];
			$data[$key]['addtime'] = $val['addtime'];
		}

		return $data;

	}

	/**
	*	首页最新商品
	*/
	public function getSellGoods()
	{
		$where = [];
		$where['g_status'] = 2;
		$where['g_type'] = 2;
		$result = Db::table('sz_goods')->where($where)->select();

		$data = [];
		foreach($result as $key=>$val){
			$list[$key] = json_decode($val['g_pic'],true);
			foreach($list as $k1=>$v1){
				$res[$key]['photos'] = $v1['photos'];
				foreach($res[$key]['photos'] as $k2=>$v2){
					$arr['photos'][$k2]['url'] = getPreviewUrl($v2['url']);
					$data[$key]['g_pic'] = json_encode($arr);
				}
			}
			$data[$key]['id'] = $val['id'];
			$data[$key]['id'] = $val['id'];
			$data[$key]['g_name'] = $val['g_name'];
			$data[$key]['g_description'] = $val['g_description'];
			$data[$key]['g_unit'] = $val['g_unit'];
			$data[$key]['g_cprice'] = $val['g_cprice'];
			$data[$key]['g_oprice'] = $val['g_oprice'];
			$data[$key]['g_type'] = $val['g_type'];
		}

		return $data;
	}

	/**
	*	会员专柜申请弹窗提示
	*/
	public function getWelfare()
	{
		$where = [];
		$where['m_name'] = '专柜申请';
		$result = Db::table('sz_counters')->where($where)->find();
		return $result;
	}


	/**
	*	会员专柜申请
	*   @param array $data  数据
	*/
	public function addCounters($data)
	{
		$user_id = Session::get('user_id');
		if(empty($user_id)){
			$url = '/home/login/login';
			$msg = '你还没有登录,请前去登录';
			return ['error' => 2,'msg' => $msg,'url' =>$url];
		}else{

			$data['uid'] = Session::get('user_id');
			$data['addtime'] = strtotime(date('Y-m-d H:i:s',time()));
			$result = Db::table('sz_counters')->insert($data);
			$msg = '申请成功,等待审核';
			$url = '';
			return ['success' => 1,'msg' =>$msg,'url'=>$url];
		}
	}

	/**
	*	会员申请为合伙人弹窗提示
	*/
	public function getPartner()
	{
		$where = [];
		$where['m_name'] = '专柜申请';
		$result = Db::table('sz_counters')->where($where)->find();
		return $result;
	}


	/**
	*	会员申请为合伙人
	*   @param array $data  数据
	*/
	public function addPartner($data)
	{
		$user_id = Session::get('user_id');
		if(empty($user_id)){
			$url = '/home/login/login';
			$msg = '你还没有登录,请前去登录';
			return ['error' => 2,'msg' => $msg,'url' =>$url];
		}else{

			$data['uid'] = Session::get('user_id');
			$data['addtime'] = strtotime(date('Y-m-d H:i:s',time()));

			$result = Db::table('sz_partnership')->insert($data);
			$msg = '申请成功,等待审核';
			$url = '';
			return ['success' => 1,'msg' =>$msg,'url'=>$url];
		}
	}

	/**
	*	团购倒计时结束隐藏	
	*   @param $id  商品id
	*/
	public function editGoodsStatus($id)
	{
		if($id){
			Db::table('sz_goods')->where('id',$id)->update(['g_status'=>3]);
			return true;
		}else{
			return false;
		}
	}

}